// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展 
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------

import WebsocketUtil from '@/common/socket.js';
import $util from '@/common/tools.js';

const app = {
	namespaced: true,
	state: {
		// 主题色
		themeColor: "#325DFF",
		// 小程序名称
		appletName: "",
		// 登录背景图
		loginImg: "",
		// 备案号声明
		statement: "",
		// 分享标题
		shareTitle: "",
		// 分享图片
		shareImage: "",
		// 版本号
		version: "",
		// 腾讯地图key
		tencentMapKey: "",
		// 当前选择城市
		currentCity: {},
		// websocket链接
		websocketPort: "",
		// 图片前缀
		domain: "",
		// 新订单提醒音频
		newOrderNotice: "",
		// 取消订单提醒音频
		cancelOrderNotice: "",
		// 司机订单监听websocket
		driverWebsocket: null,
		// 通知音频播放状态 1.正在播放，2.未播放
		orderAudioStatus: 2,
		// 音频播放重置定时器
		orderAudioTimer: null,
	},
	mutations: {
		// 设置基础配置
		setConfig(state, data) {
			state.themeColor = data.themeColor || "#325DFF"
			state.appletName = data.appletName || ""
			state.loginImg = data.loginImg || ""
			state.statement = data.statement || ""
			state.shareTitle = data.shareTitle || ""
			state.shareImage = data.shareImage || ""
			state.version = data.version || "1.0.0"
			// state.tencentMapKey = data.tencentMapKey || ""
			state.tencentMapKey = "48a0d64a4a45d23512f3e7733cda4cfa"
			state.websocketPort = data.websocketPort || ""
			state.domain = data.domain || ""
			state.newOrderNotice = data.newOrderNotice || ""
			state.cancelOrderNotice = data.cancelOrderNotice || ""
		},
		// 设置选择城市
		setCurrentCity(state, data) {
			if (data) {
				uni.setStorageSync('currentCity', data)
				state.currentCity = data
			} else {
				state.currentCity = {
					province: "北京市",
					city: "北京市",
					latitude: 39.904179,
					longitude: 116.40738,
				}
			}
		},
		// 设置司机端订单状态监听
		setDriverSocket(state, type) {
			if (type == 1) {
				if (!state.websocketPort) {
					setTimeout(() => {
						this.commit('app/setDriverSocket', type)
					}, 200);
					return
				}
				if (state.driverWebsocket && state.driverWebsocket.isOpen) {
					state.driverWebsocket.destroy();
				}
				state.driverWebsocket = new WebsocketUtil('wss://' + state.websocketPort, 5);
				state.driverWebsocket.onMessage(res => {
					if (res.type == "init") {
						$util.request("main.socket.bindCancel", { client_id: res.client_id }).then(result => {
							if (result.code != 1) $util.request("main.socket.bindCancel", { client_id: res.client_id })
						})
					} else if (res.type == "userCancelOrder") {
						if (state.orderAudioStatus == 2) {
							state.orderAudioStatus = 1
							if (state.orderAudioTimer) clearTimeout(state.orderAudioTimer)
							state.orderAudioTimer = setTimeout(() => {
								state.orderAudioStatus = 2
							}, 5000);
							var soundAndVibrate = uni.getStorageSync("soundAndVibrate")
							if (!soundAndVibrate) {
								soundAndVibrate = {
									soundOpen: true,
									vibrateOpen: true,
								}
							}
							if (soundAndVibrate.vibrateOpen) uni.vibrateLong();
							if (soundAndVibrate.soundOpen) {
								const innerAudioContext = uni.createInnerAudioContext();
								innerAudioContext.autoplay = true;
								innerAudioContext.src = state.cancelOrderNotice;
								innerAudioContext.onEnded(() => {
									state.orderAudioStatus = 2
								});
							}
						}
					}
				});
			} else if (state.driverWebsocket && state.driverWebsocket.isOpen) {
				state.driverWebsocket.destroy();
				state.orderAudioStatus = 2
			}
		},
	},
}

export default app